/*
 * 文件名：ShopInfoService.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：zhangshaohua
 * 修改时间：2016年4月15日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.service.base.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.base.BusinessShopAccount;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.CellShopVo;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.base.ShopInfoView;
import com.zcsy.manage.persistence.base.ShopInfoDao;
import com.zcsy.manage.service.base.IShopInfoService;
import com.zcsy.manage.service.common.CommonService;
import com.zcsy.manage.service.system.impl.SystemComService;

@Service
public class ShopInfoService extends SystemComService implements IShopInfoService{

	@Autowired
	private ShopInfoDao shopInfoDao;
	
	@Override
	public int insert(ShopInfo shopInfo) {
	    shopInfo.setSelfStore(Constants.SELFSTORE_N);
		return shopInfoDao.insert(shopInfo);
	}

	@Override
	public int update(ShopInfo shopInfo) {
		return shopInfoDao.update(shopInfo);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		return shopInfoDao.deleteByPrimaryKey(id);
	}

	@Override
	public ShopInfo selectByPrimaryKey(String id) {
		return shopInfoDao.selectByPrimaryKey(id);
	}

	@Override
	public List<ShopInfoView> selectByKeyword(ShopInfo shopInfo) {
		return shopInfoDao.selectByKeyword(shopInfo);
	}
	
	@Override
	public List<ShopInfo> getShopList(ShopInfo shopInfo) {
	    return shopInfoDao.getShopList(shopInfo);
	}

	@Override
	public List<ShopInfo> getAllShopInfo() {
		return shopInfoDao.getAllShopInfo();
	}

    @Override
    public int updateShopState(ShopInfo shopInfo) {
        return shopInfoDao.updateShopState(shopInfo);
    }

	@Override
	public Set<Map<String, String>> getAllCellShopInfo(String[] roles) {
		
        List<CellShopVo> list = shopInfoDao.getAllCellShopInfo(roles);
        Set<Map<String, String>> listcell = new HashSet<Map<String,String>>();
        
        List<CellInfo> userCell = CommonService.getSessionUser().getCellInfos();
        
        for(CellShopVo item : list){
            //当前用户自己有权限的小区才可选中
            boolean have = false;
            for(CellInfo j : userCell){
            	if(item.getCellId().equals(j.getId())){
            		have = true;
            		break;
            	}
            }
        	
        	Map<String, String> mapshop = new HashMap<String, String>();
        	Map<String, String> mapcell = new HashMap<String, String>();
        	Map<String, String> mapcity = new HashMap<String, String>();
        	Map<String, String> mapprovince = new HashMap<String, String>();
    	    mapshop.put("id", item.getShopId());
    	    mapshop.put("pId", item.getCellId());
    	    mapshop.put("name", item.getShopName());
    	    mapshop.put("chkDisabled", "false");
    	    if(!have){
    	    	mapshop.put("chkDisabled", "true");//用户没权限的小区就不能编辑
    	    }
    	    listcell.add(mapshop);
    	
        	mapcell.put("id", item.getCellId());
        	mapcell.put("pId", item.getCityId());
        	mapcell.put("name", item.getCellName());
        	mapcell.put("chkDisabled", "false");
        	listcell.add(mapcell);
        	
        	mapcity.put("id", item.getCityId());
        	mapcity.put("pId", item.getProvinceId());
        	mapcity.put("name", item.getCityName());
        	mapcity.put("chkDisabled", "false");
        	listcell.add(mapcity);
        	
        	mapprovince.put("id", item.getProvinceId());
        	mapprovince.put("pId", "0");
        	mapprovince.put("name", item.getProvinceName());
        	mapprovince.put("chkDisabled", "false");
        	listcell.add(mapprovince);
        	
        }
		return listcell;
	}

	@Override
	public void updateUserShop(String userId, String[] shops) {

		if(userId != null){
			shopInfoDao.deleteShopByUserId(userId);
			if(shops != null && shops.length > 0){
				for (String shopId : shops) {
					BusinessShopAccount record = new BusinessShopAccount();
					record.setId(UniqueNumberUtils.get18UniqueNumber());
					record.setBusiAccountId(userId);
					record.setShopId(shopId);
					shopInfoDao.insertShopAccount(record);
				}
			}
		}
	}

	@Override
	public List<String> getShopIdsByUserId(String userId) {
		if(StringUtils.isEmpty(userId) || userId.equals(Constants.ID_ZERO)){
			return new ArrayList<String>();
		}
		
		return shopInfoDao.getShopIdsByUserId(userId);
	}

	@Override
	public 	List<Map<String, Object>> getCellIdListByBusiAccountId(String busiAccountId) {
		return shopInfoDao.getCellIdListByBusiAccountId(busiAccountId);
	}

	@Override
	public List<ShopInfo> getListByCellIdAndCategoryId(String cellId,
			String categoryId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cellId", cellId);
		map.put("categoryId", categoryId);
		return shopInfoDao.getListByCellIdAndCategoryId(map);
	}
	
	@Override
	public List<ShopInfo> getShopInfoByName(String name, String cellId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("cellId", cellId);
		return shopInfoDao.getShopInfoByName(map);
	}

	@Override
    public List<String> getSelfStoreIdBycellId(String cellId) {
       return shopInfoDao.getSelfStoreIdBycellId(cellId);
    }
	
	@Override
	public List<ShopInfo> selectShopInfoByAdCategoryId(String adCategoryId , String cellId) {
	    Map<String, Object> map = new HashMap<String, Object>();
        map.put("cellId", cellId);
        map.put("adCategoryId", adCategoryId);
	    return shopInfoDao.selectShopInfoByAdCategoryId(map);
	}

	@Override
	public List<ShopInfo> getStoreByCellId(String cellId) {
		return shopInfoDao.getStoreByCellId(cellId);
	}

    @Override
    public List<ShopInfoView> selectByKeywords(ShopInfo shopInfo) {
        return shopInfoDao.selectByKeywords(shopInfo);
    }

    @Override
    public Integer countApply(String cellId) {
        return shopInfoDao.countApply(cellId);
    }

}
